var common = require('../../lib/common.js')
var util_question = require('../../lib/question.js')
const db = wx.cloud.database()
const app = getApp()

Page({

  /**
   * 页面的初始数据
   */
  data: {
    checkpoint: null,
    question: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let task = app.globalData.task
    console.log('question onlod', task)
    if (!('qid' in task)) {
      console.log('no')
      // 跳转到关卡显示
      wx.navigateTo({
        url: '../checkpoint/checkpoint'
      })
      return
    }
    let checkpoint_list = app.globalData.config.checkpoint_list
    let question_list = app.globalData.config.question_list
    let question = question_list[task.qid]
    let checkpoint = checkpoint_list[task.cid]

    // 显示题目内容
    this.setData({
      checkpoint: checkpoint,
      question: question,
      answer: 'answer',
    })

  },

  onTest: function(e) {
    util_question.set_question(5180)
  },

  // 回答问题
  onAnswerQuestion: function(e) {
    let next_question_id = util_question.answer_question(this.data.answer)
    console.log('next_question_id', next_question_id)
    // 页面刷新
    this.onLoad()
    if (next_question_id > 0) {}
  },

  // 放弃任务
  onAbortCheckport: function(e) {
    // 放弃关卡
    common.abort_checkpoint(this.data.checkpoint.id)

    // 跳转到关卡显示
    wx.navigateTo({
      url: '../checkpoint/checkpoint'
    })
  },

})